
#include "StdAfx.h"
#include "CustomMsgCenter.h"
#include "../Common/CommonDef.h"

extern volatile BOOL g_bAppQuit;

CCustomMsgCenter::CCustomMsgCenter()
{
	m_hMessageWnd		= NULL;
	m_oldMsgProc		= NULL;
	
	m_nCurBlankMsgText	= 0;
	ZERO_ARRAY(m_szMsgTextBuf);
}

CCustomMsgCenter::~CCustomMsgCenter()
{
	
}

BOOL CCustomMsgCenter::Init()
{
	//  Create message window
	m_hMessageWnd = CreateWindow( _T("Message"), _T(""), WS_POPUP, 0, 0, 0, 0, HWND_MESSAGE, NULL , GetModuleHandle(NULL), 0 );
	if( NULL != m_hMessageWnd )
	{
		::SetWindowLong( m_hMessageWnd, GWL_USERDATA, (LONG)this );
		m_oldMsgProc =(WNDPROC)::SetWindowLong( m_hMessageWnd, GWL_WNDPROC, (LONG)MessageWindowProc);
	}

	return TRUE;
}

CCustomMsgCenter::Uninit()
{
	if( NULL != m_hMessageWnd )
	{
		::SetWindowLong( m_hMessageWnd, GWL_USERDATA, NULL);
		::SetWindowLong( m_hMessageWnd, GWL_WNDPROC, (LONG)m_oldMsgProc);
		m_oldMsgProc = NULL;

		DestroyWindow( m_hMessageWnd );
		m_hMessageWnd = NULL;
	}

	return TRUE;
}


BOOL CCustomMsgCenter::RegCustomMsgProcess(DWORD dwMsg, CCustMsgBase* pThis, MSG_PROCESS_FUNC pMsgProc)
{
	if(dwMsg == 0 || pThis == NULL || pMsgProc == NULL)
	{
		ASSERT(FALSE);
		return FALSE;
	}

	MSGPROCESSFUNC mpf = {0};
	mpf.dwMsg = dwMsg;
	mpf.pThis = pThis;
	mpf.pFunc = pMsgProc;
	m_vecMsgFunc.push_back(mpf);
	
	return TRUE;
}

BOOL CCustomMsgCenter::RemoveCustomMsgProcess(DWORD dwMsg, CCustMsgBase* pThis, MSG_PROCESS_FUNC pMsgProc)
{
	for(int i = 0; i < m_vecMsgFunc.size(); i++)
	{
		if(m_vecMsgFunc[i].dwMsg == dwMsg && m_vecMsgFunc[i].pThis == pThis && m_vecMsgFunc[i].pFunc == pMsgProc)
		{
			VECREGMSGFUNC::iterator iter=m_vecMsgFunc.begin() + i;
			m_vecMsgFunc.erase(iter);
			break;
		}
	}
	
	return TRUE;
}
BOOL CCustomMsgCenter::RegTimerProcess(	DWORD dwTimerID,	CCustMsgBase* pThis,	MSG_PROCESS_FUNC pMsgProc)
{
	if(dwTimerID == 0 || pThis == NULL || pMsgProc == NULL)
	{
		ASSERT(FALSE);
		return FALSE;
	}

	MSGPROCESSFUNC mpf = {0};
	mpf.dwMsg = WM_TIMER;
	mpf.pThis = pThis;
	mpf.pFunc = pMsgProc;
	mpf.dwTimerID = dwTimerID;
	m_vecMsgFunc.push_back(mpf);
	
	return TRUE;

}

BOOL CCustomMsgCenter::RemoveTimerProcess(	DWORD dwTimerID,	CCustMsgBase* pThis,	MSG_PROCESS_FUNC pMsgProc)
{
	for(int i = 0; i < m_vecMsgFunc.size(); i++)
	{
		if(m_vecMsgFunc[i].dwMsg == WM_TIMER && m_vecMsgFunc[i].dwTimerID == dwTimerID && m_vecMsgFunc[i].pThis == pThis && m_vecMsgFunc[i].pFunc == pMsgProc)
		{
			VECREGMSGFUNC::iterator iter=m_vecMsgFunc.begin() + i;
			m_vecMsgFunc.erase(iter);
			break;
		}
	}

	return TRUE;
}


BOOL CCustomMsgCenter::PostCustMessage(DWORD dwMsg, WPARAM wParam, LPARAM lParam)
{
	if(m_hMessageWnd)
		return PostMessage(m_hMessageWnd, dwMsg, wParam, lParam);

	return TRUE;
}

BOOL CCustomMsgCenter::SendCustMessage(DWORD dwMsg, WPARAM wParam, LPARAM lParam)
{
	if(m_hMessageWnd && !g_bAppQuit)
		return SendMessage(m_hMessageWnd, dwMsg, wParam, lParam);
	
	return TRUE;
}

BOOL CCustomMsgCenter::StartTimer(DWORD dwTimerID, DWORD dwElapse)
{
	if(m_hMessageWnd)
		SetTimer(m_hMessageWnd, dwTimerID, dwElapse, NULL);

	return TRUE;
}

BOOL CCustomMsgCenter::StopTimer(DWORD dwTimerID)
{
	if(m_hMessageWnd)
		KillTimer(m_hMessageWnd, dwTimerID);
	
	return TRUE;
}

LRESULT CCustomMsgCenter::MessageWindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	CCustomMsgCenter* pThis= (CCustomMsgCenter*)::GetWindowLong(hWnd, GWL_USERDATA);
	if(pThis == NULL)
		return 0;

	for(int i = 0; i < pThis->m_vecMsgFunc.size(); i++)
	{
		if(message == WM_TIMER && pThis->m_vecMsgFunc[i].dwTimerID == wParam || message != WM_TIMER && pThis->m_vecMsgFunc[i].dwMsg == message)
		{
			CCustMsgBase* pClassInst = pThis->m_vecMsgFunc[i].pThis;
			MSG_PROCESS_FUNC pClassFunc = pThis->m_vecMsgFunc[i].pFunc;
			
			if(pClassInst && pClassFunc)
				(pClassInst->*pClassFunc)(wParam, lParam);
		}
	}

	return 0;
}

BOOL CCustomMsgCenter::PostStringMessage(DWORD dwMsg, CString strMsgString, LPARAM lParam)
{
	_tcscpy(m_szMsgTextBuf[m_nCurBlankMsgText], strMsgString);
	PostCustMessage(dwMsg, (WPARAM)m_szMsgTextBuf[m_nCurBlankMsgText], lParam);
	
	m_nCurBlankMsgText++;
	if(m_nCurBlankMsgText >= MSG_TEXT_BUF_COUNT)
		m_nCurBlankMsgText = 0;

	return TRUE;
}